package client;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class MATM_Client {

	/**
	 * This Class defines the client that can send and receive data from the
	 * server
	 * 
	 * @author XA9
	 */

	private Socket client;
	private DataOutputStream output;
	private BufferedReader input;
	private int port;
	private String hostname;
	byte key = 7;

	/**
	 * @param hostname
	 * @param port
	 * @throws IOException
	 * @throws IOException
	 */

	public MATM_Client(String hostname, int port) throws IOException,
			IOException {
		this.hostname = hostname;
		this.port = port;
	}

	/**
	 * Creates a new Socket on the port given matching the server to get a
	 * connection
	 * 
	 * @param msg
	 * @return
	 * @throws IOException
	 */

	public String send(String msg) throws IOException {
		this.client = new Socket(hostname, port);

		this.output = new DataOutputStream(this.client.getOutputStream());

		this.input = new BufferedReader(new InputStreamReader(
				this.client.getInputStream()));

		/**
		 * String msg gets written to a newMsg byte The byte newMsg gets run
		 * through a for loop with the XOR operator in the factor of the given
		 * key' Gets encrypted
		 */

		byte[] newMsg = msg.getBytes();

		for (int i = 0; i < newMsg.length; i++) {
			newMsg[i] = (byte) (newMsg[i] ^ key);
			System.out.println(newMsg);
		}
		this.output.writeBytes(msg + '\n');

		String resp = this.input.readLine();

		/**
		 * String resp gets written to a newResp byte The byte newResp gets run
		 * through a for loop with the XOR operator in the factor of the given
		 * key gets decrypted
		 * 
		 * @return resp
		 */

		byte[] newResp = resp.getBytes();

		for (int i = 0; i < newResp.length; i++) {
			newResp[i] = (byte) (newResp[i] ^ key);
		}
		System.out.println(resp);

		return resp;
	}

	/**
	 * Causes the client to stop running
	 * 
	 * @throws IOException
	 */

	public void stop() throws IOException {
		this.client.close();
	}

	/**
	 * Sends the users input to the server
	 * 
	 * @param account
	 * @return
	 * @throws NumberFormatException
	 * @throws IOException
	 */

	public double getBalance(String account) throws NumberFormatException,
			IOException {
		return Double.parseDouble(this.send("BALANCE:" + account));
	}

	public double getBalanceDkk(String account) throws NumberFormatException,
			IOException {
		return Double.parseDouble(this.send("BALANCEDKK:" + account));
	}

	public boolean auth(String account, String password) throws IOException {
		return Boolean.parseBoolean(this.send("AUTH:" + account + ":"
				+ password));
	}

	public void depositDkk(String account, double money) throws IOException {
		this.send("DEPOSITDKK:" + account + ":" + money);
	}

	public void depositBtc(String account, double money) throws IOException {
		this.send("DEPOSITBTC:" + account + ":" + money);
	}

	public void sendBitCoins(String account, double amount) throws IOException {
		this.send("SENDB:" + account + ":" + amount);
	}

}